package edu.bw.web.user;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.service.UserService;
import edu.bw.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/user/deleteUserByIds")
public class DeleteUserServlet extends HttpServlet {

    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //{"ids":[29,2,21]}
        String reqJSON = request.getReader().readLine();
        List<Integer> ids = JSONUtil.parseArray(JSONUtil.parseObj(reqJSON).get("ids")).toList(Integer.class);

        Integer rows = userService.deleteUserByIds(ids);

        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}